package com.ebupt.migu.music.autotri.controller;

import com.ebupt.migu.music.autotri.entity.ResultMsg;
import com.ebupt.migu.music.autotri.service.AutoTriService;
import com.ebupt.migu.music.autotri.service.EffectTotalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author tz
 */
@Slf4j
@Component
@RestController
@EnableScheduling
@RequestMapping("/autoTri")
public class AutoTriTask {

    @Autowired
    private AutoTriService autoTriService;
    @Autowired
    private EffectTotalService effectTotalService;

    @RequestMapping("/do")
    public ResultMsg autoTri(@RequestBody Map<String, Object> param) {
        return autoTriService.autoTri(param);
    }

    @RequestMapping("/refresh/channels")
    public ResultMsg refreshChannels() {
        autoTriService.channels();
        ResultMsg res = new ResultMsg();
        res.setCode("0000");
        return res;
    }

    @Scheduled(cron = "${popup.tri_time}")
    public void autoPopup() {
        log.info("弹窗策略--定时任务启动");
        autoTriService.autoPopup();
        log.info("弹窗策略--定时任务结束");
    }

    @Scheduled(cron = "${push.httpPushResult}")
    public void autoPushTotal() {
        log.info("push效果统计--定时任务启动");
        effectTotalService.autoPushTotal();
        log.info("push效果统计--定时任务结束");
    }

    @Scheduled(cron = "${msg.ftpMsgResult}")
    public void autoMsgTotal() {
        log.info("消息通知效果统计--定时任务启动");
        effectTotalService.autoMsgTotal();
        log.info("消息通知效果统计--定时任务结束");
    }

    @Scheduled(cron = "${status.start}")
    public void statusToGoing() {
        log.info("策略状态--未开始");
        effectTotalService.statusToGoing();
        log.info("策略状态--进行中");
    }

    @Scheduled(cron = "${status.end}")
    public void statusToEnd() {
        log.info("策略状态--进行中");
        effectTotalService.statusToEnd();
        log.info("策略状态--已结束");
    }

    /**
     * 每天上午十点刷新一次渠道白名单
     */
    @Scheduled(cron = "0 0 10 * * ?")
    public void channels() {
        autoTriService.channels();
    }
}
